package com.wind.trademark.task.dao.impl;


// default package

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.simple.ParameterizedBeanPropertyRowMapper;
import org.springframework.stereotype.Repository;

import com.wind.trademark.base.model.BsCategory;
import com.wind.trademark.common.dao.BaseDao;
import com.wind.trademark.common.vo.ComboBean;
import com.wind.trademark.common.vo.PageBean;
import com.wind.trademark.common.vo.PageParam;
import com.wind.trademark.task.model.TkAttachment;
import com.wind.trademark.task.model.TkImg;
import com.wind.trademark.task.model.TkTaskStatus;
import com.wind.trademark.task.vo.TaskLogVo;
import com.wind.trademark.task.vo.TaskProfile;
import com.wind.trademark.task.vo.TaskVo;





/**
 * @author  <a href="mailto:xmufeng@163.com">Wind Wu</a>
 *
 */
@Repository
public class TaskDaoImpl implements com.wind.trademark.task.dao.TaskDao {
	@Autowired
	private BaseDao baseDao;

	@Override
	public PageBean queryTask(PageParam pageParam,  Map<String,Object> params) {
		
		String sql= TaskSql.queryTaskSQL(params);
		PageBean pageBean=baseDao.queryPageBeanAndMapping(sql, pageParam,TaskVo.class, params);

		return pageBean;
	}

	@Override
	public void removeAllCategoryWithTask(Long taskId) {
		String sql= TaskSql.removeAllCategoryWithTask();
		Map<String,Object> paramMap=new HashMap<String, Object>();
		paramMap.put("taskId", taskId);
		baseDao.getNamedParameterJdbcTemplate().update(sql, paramMap);
	}

	@Override
	public List<BsCategory> getL3CategoriesByTaskId(Long taskId) {
		
		String sql= TaskSql.getL3CategoriesByTaskId();
		RowMapper<BsCategory> rowMapper = ParameterizedBeanPropertyRowMapper.newInstance(BsCategory.class);

		List<BsCategory> result = baseDao.getNamedParameterJdbcTemplate().query(sql,new MapSqlParameterSource().addValue("taskId", taskId), rowMapper);

		return result;
	}

	
	@Override
	public TaskProfile getTask4Bill(Long taskId) {
		
		String sql= TaskSql.getTask4Bill();
		RowMapper<TaskProfile> rowMapper = ParameterizedBeanPropertyRowMapper.newInstance(TaskProfile.class);

		TaskProfile result = baseDao.getNamedParameterJdbcTemplate().queryForObject(sql,new MapSqlParameterSource().addValue("taskId", taskId), rowMapper);

		return result;
	}

	@Override
	public PageBean getLogListByTaskId(PageParam pageParam, Long taskId) {
		
		Map<String, Object> params=new HashMap<String, Object>();
		params.put("taskId", taskId);
		String sql= TaskSql.getLogListByTaskId();
		PageBean pageBean=baseDao.queryPageBeanAndMapping(sql, pageParam,TaskLogVo.class, params);

		return pageBean;
		
	}

	@Override
	public List<ComboBean> getAllTask() {
		String sql= TaskSql.getAllTaskSQL();
		RowMapper<ComboBean> rowMapper = ParameterizedBeanPropertyRowMapper.newInstance(ComboBean.class);
		List<ComboBean> result = baseDao.getNamedParameterJdbcTemplate().query(sql,new MapSqlParameterSource(), rowMapper);
		return result;
	}

	@Override
	public List<ComboBean> goolgeTask(Map<String, Object> params) {
		String sql= TaskSql.goolgeTaskSQL(params);
		RowMapper<ComboBean> rowMapper = ParameterizedBeanPropertyRowMapper.newInstance(ComboBean.class);
		List<ComboBean> result = baseDao.getNamedParameterJdbcTemplate().query(sql,params, rowMapper);
		return result;
	}

 
	@Override
	public List<TkTaskStatus> findNextStepsTaskStatus(List<String> nextSteps) {
		String sql= TaskSql.findNextStepsTaskStatus();
		RowMapper<TkTaskStatus> rowMapper = ParameterizedBeanPropertyRowMapper.newInstance(TkTaskStatus.class);
		List<TkTaskStatus> result = baseDao.getNamedParameterJdbcTemplate().query(sql,new MapSqlParameterSource().addValue("nextSteps", nextSteps), rowMapper);
		return result;
	}

	@Override
	public TkTaskStatus getCurrentStatusByTaskId(Long taskId) {
		String hql="select tkTaskStatus from  TkTaskStatus  tkTaskStatus  ,  TkTask  tkTask where tkTaskStatus.code=tkTask.status and tkTask.id=:taskId";
		Query query=this.baseDao.getHibernateTemplate().getSessionFactory().getCurrentSession().createQuery(hql);
		query.setLong("taskId", taskId);
		TkTaskStatus tkTaskStatus=(TkTaskStatus) query.uniqueResult();
		return tkTaskStatus;
	}

	@Override
	public List<TkImg> getImageListByTaskId(Long taskId) {
		return this.baseDao.findByProperty(TkImg.class, "taskId", taskId);
	}
	
	@Override
	public List<TkAttachment> getAttachmentListByTaskId(Long taskId) {
		return this.baseDao.findByProperty(TkAttachment.class, "taskId", taskId);
	}

	@Override
	public void deleteTkImg(List<Long> ids) {
		String sql= TaskSql.deleteTkImg();
		baseDao.getNamedParameterJdbcTemplate().update(sql,new MapSqlParameterSource().addValue("ids",ids));
	}

	@Override
	public void deleteTkAttachment(List<Long> ids) {
		String sql= TaskSql.deleteTkAttachment();
		baseDao.getNamedParameterJdbcTemplate().update(sql,new MapSqlParameterSource().addValue("ids",ids));
	}
	
}